package com.sls.web.manager.action.recharge;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.Area;
import com.sls.web.manager.recharge.pojo.SupplierRechargeMode;
import com.sls.web.manager.recharge.pojo.SystemProduct;
import com.sls.web.manager.recharge.querybean.AreaQuery;
import com.sls.web.manager.recharge.querybean.SupplierRechargeModeQuery;
import com.sls.web.manager.recharge.service.AreaService;
import com.sls.web.manager.recharge.service.SupplierAccountService;
import com.sls.web.manager.recharge.service.SupplierRechargeModeService;
import com.sls.web.manager.recharge.service.SystemProductService;

public class SupplierRechargeModeAction extends SuperAction {
	private static final long serialVersionUID = 4361068469185173283L;
	@Resource(name = "supplierRechargeModeService")
	private SupplierRechargeModeService supplierRechargeModeService;
	@Resource(name = "supplierAccountService")
	private SupplierAccountService supplierAccountService;
	@Resource(name = "systemProductService")
	private SystemProductService systemProductService;
	@Resource(name = "areaService")
	private AreaService areaService;

	/**
	 * 返回分页对象
	 */
	public Pagination<?> page;
	private SupplierRechargeModeQuery supplierRechargeModeQuery;
	private AreaQuery areaQuery;
	private SupplierRechargeMode supplierRechargeMode;
	private String id;
	private String startTime;
	private String endTime;
	private String minAmount;
	private String maxAmount;
	private List<Area> areaAble;

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public SupplierRechargeModeQuery getSupplierRechargeModeQuery() {
		return supplierRechargeModeQuery;
	}

	public void setSupplierRechargeModeQuery(
			SupplierRechargeModeQuery supplierRechargeModeQuery) {
		this.supplierRechargeModeQuery = supplierRechargeModeQuery;
	}

	public SupplierRechargeMode getSupplierRechargeMode() {
		return supplierRechargeMode;
	}

	public void setSupplierRechargeMode(
			SupplierRechargeMode supplierRechargeMode) {
		this.supplierRechargeMode = supplierRechargeMode;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public AreaQuery getAreaQuery() {
		return areaQuery;
	}

	public void setAreaQuery(AreaQuery areaQuery) {
		this.areaQuery = areaQuery;
	}

	public List<Area> getAreaAble() {
		return areaAble;
	}

	public void setAreaAble(List<Area> areaAble) {
		this.areaAble = areaAble;
	}

	public String getMinAmount() {
		return minAmount;
	}

	public void setMinAmount(String minAmount) {
		this.minAmount = minAmount;
	}

	public String getMaxAmount() {
		return maxAmount;
	}

	public void setMaxAmount(String maxAmount) {
		this.maxAmount = maxAmount;
	}

	// 添加
	public String add() throws Exception {
		supplierRechargeMode = new SupplierRechargeMode();
		supplierRechargeMode.setProductList(systemProductService
				.queryListProductTypeFling(this.getCurrentOperator()));
		supplierRechargeMode.setAccountList(supplierAccountService.list(this
				.getCurrentOperator()));
		return "edit";
	}

	// 添加供货商提供的充值方式
	public String addMode() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		BigDecimal bigDecimal = new BigDecimal(1000);
		try {
			if (StringUtils.isNotEmpty(minAmount))
				supplierRechargeMode.setMinAmount(new BigDecimal(minAmount)
						.multiply(bigDecimal).intValue());
			if (StringUtils.isNotEmpty(maxAmount))
				supplierRechargeMode.setMaxAmount(new BigDecimal(maxAmount)
						.multiply(bigDecimal).intValue());
			if (StringUtils.isNotBlank(startTime))
				supplierRechargeMode.setStartTime(sdf.parse(startTime));
			if (StringUtils.isNotBlank(endTime))
				supplierRechargeMode.setEndTime(sdf.parse(endTime));
			SystemProduct sprodcut = systemProductService.queryOneById(
					supplierRechargeMode.getSystemProductId(),
					this.getCurrentOperator());
			supplierRechargeMode.setOperationCode(sprodcut.getOperationCode());
			supplierRechargeMode.setPhoneType(sprodcut.getPhoneType());
			supplierRechargeModeService.save(supplierRechargeMode,
					this.getCurrentOperator());
			redirectionUrl = "supplier_recharge_mode!list.htm";
			return "success";
		} catch (ManagerException e) {
			supplierRechargeMode = null;
			addActionError(e.getMessage());
			return this.add();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.add();
		}

	}

	// 列表
	public String list() {
		if (supplierRechargeModeQuery == null) {
			supplierRechargeModeQuery = new SupplierRechargeModeQuery();
			supplierRechargeModeQuery.setPageSize(Constants.INIT_PAGESIZE);
			supplierRechargeModeQuery
					.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = supplierRechargeModeService.queryList(
					supplierRechargeModeQuery, getCurrentOperator());
			if (page != null && page.getList() != null
					&& !page.getList().isEmpty()) {
				for (Iterator<?> it = page.getList().iterator(); it.hasNext();) {
					SupplierRechargeMode mode = (SupplierRechargeMode) it
							.next();
					areaQuery = new AreaQuery();
					areaQuery.setSupplierRechargeModeId(mode.getId());
					mode.setAreaList(areaService.queryList(areaQuery,
							this.getCurrentOperator()));
				}

			}
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error(e.getMessage(),e);
		}

		return "list";
	}

	// 编辑
	public String edit() throws Exception {
		if (id != null) {
			supplierRechargeMode = supplierRechargeModeService.queryById(id,
					this.getCurrentOperator());
			supplierRechargeMode.setAccountList(supplierAccountService
					.list(this.getCurrentOperator()));
			supplierRechargeMode.setProductList(systemProductService
					.queryListProductTypeFling(this.getCurrentOperator()));
			if (supplierRechargeMode.getMinAmount() != null
					&& supplierRechargeMode.getMinAmount() > 0)
				supplierRechargeMode.setMinAmount(supplierRechargeMode
						.getMinAmount());
			if (supplierRechargeMode.getMaxAmount() != null
					&& supplierRechargeMode.getMaxAmount() > 0)
				supplierRechargeMode.setMaxAmount(supplierRechargeMode
						.getMaxAmount());
		}
		return "edit";
	}

	// 编辑供货商提供的充值方式
	public String editMode() throws Exception {
		BigDecimal bigDecimal = new BigDecimal(1000);
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		if (StringUtils.isNotEmpty(minAmount))
			supplierRechargeMode.setMinAmount(new BigDecimal(minAmount)
					.multiply(bigDecimal).intValue());
		if (StringUtils.isNotEmpty(maxAmount))
			supplierRechargeMode.setMaxAmount(new BigDecimal(maxAmount)
					.multiply(bigDecimal).intValue());
		if (StringUtils.isNotBlank(startTime))
			supplierRechargeMode.setStartTime(sdf.parse(startTime));
		if (StringUtils.isNotBlank(endTime))
			supplierRechargeMode.setEndTime(sdf.parse(endTime));
		SystemProduct sprodcut = systemProductService.queryOneById(
				supplierRechargeMode.getSystemProductId(),
				this.getCurrentOperator());
		supplierRechargeMode.setOperationCode(sprodcut.getOperationCode());
		supplierRechargeMode.setPhoneType(sprodcut.getPhoneType());
		boolean result = supplierRechargeModeService.update(
				supplierRechargeMode, this.getCurrentOperator());
		if (!result) {
			addActionError("修改失败!");
			return this.edit();
		} else {
			redirectionUrl = "supplier_recharge_mode!list.htm";
			return "success";
		}
	}

	// 删除
	public String delete() throws Exception {
		try{
			supplierRechargeModeService.delete(id,getCurrentOperator());
			redirectionUrl = "supplier_recharge_mode!list.htm";
			return "success";
		}catch (ManagerException e){
			addActionError(e.getMessage());
			return this.list();
		}
	}

	public String findAreaList() throws Exception {
		areaQuery = new AreaQuery();
		areaQuery.setSupplierRechargeModeId(id);
		areaAble = areaService.queryList(areaQuery, this.getCurrentOperator());
		return "json";
	}
}